/*
 * Copyright (C) 2008  Young,Fey <fey.young@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 
package com.youngfey.wordmaster.database.tables

import com.youngfey.database.annotation.*
import com.youngfey.wordmaster.database.*
import static com.youngfey.wordmaster.database.Database.*

public class ArticleText {

    Article article

    @Size(2048)
    String segment
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code (by gRDB)">
    Integer id
    

    boolean equals(Object o) {
        id != null && o instanceof ArticleText && id == o.getId()
    }

    static createTable() {
        execute("CREATE TABLE ArticleText (Id INT NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT ARTICLETEXT_PK PRIMARY KEY, Article INT NOT NULL, Segment VARCHAR(2048) NOT NULL)")
        
    }

    boolean isValid() {
        article != null && segment?.size() <= 2048
    }



    static ArticleText create(article, segment) {
        def instance = new ArticleText(article: article, segment: segment)
        instance.insert()
        instance
    }

    def insert() {
        if(id == null && isValid()) {
            id = executeInsert("INSERT INTO ArticleText (Article, Segment) VALUES (${article?.getId()}, ${segment})")[0][0]
            if(id != null) {
                
                return
            }
        }

        throw new IllegalStateException()
    }

    //capitalize;
    static ArticleText find(Integer id) {
        if(id == null) return null

        

        def row = firstRow("SELECT * FROM ArticleText WHERE Id = $id")
        
        row ? new ArticleText(id: row.Id, article: Article.find((Short)row.article), segment: row.segment) : null
        
    }

    static ArrayList<ArticleText> findBySql(stmt) {
        def rows = []
        eachRow(stmt) {row ->
            rows << new ArticleText(id: row.Id, article: Article.find((Short)row.article), segment: row.segment)
        }
        
        rows
    }

    static ArrayList<ArticleText> findAll() {
        findBySql('SELECT * FROM ArticleText')
    }

    
    static ArrayList<ArticleText> findAll(String keyword) {
        findBySql("SELECT * FROM ArticleText WHERE (Segment LIKE $keyword)")
    }
    

    private executeUpdate(stmt) {
        def updated = executeUpdate(stmt)
        if(updated != 1) throw new IllegalStateException()
        
    }

    def update() {
        if(id != null && isValid()) {
            executeUpdate("UPDATE ArticleText SET Article = ${article?.getId()}, Segment = ${segment} WHERE Id = $id")
        }
    }

    static delete(Integer id) {
        execute("DELETE FROM ArticleText WHERE Id = $id")
        
    }

    def delete() {
        if(id != null) delete(id)
    }


    static ArrayList<ArticleText> findByArticle(value) {
        findBySql("SELECT * FROM ArticleText WHERE Article = ${value?.getId()}")
    }

    static ArrayList<ArticleText> findBySegment(value) {
        findBySql("SELECT * FROM ArticleText WHERE Segment = ${value}")
    }



    static ArrayList<ArticleText> findByArticleLike(String keyword) {
        findBySql("SELECT * FROM ArticleText WHERE Article LIKE $keyword")
    }

    static ArrayList<ArticleText> findBySegmentLike(String keyword) {
        findBySql("SELECT * FROM ArticleText WHERE Segment LIKE $keyword")
    }



    def updateArticle(value) {
        this.article = value

        if(id != null && isValid()) {
            executeUpdate("UPDATE ArticleText SET Article = ${value?.getId()} WHERE Id = $id")
        } else {
            throw new IllegalStateException("Update Error: value = $value")
        }
    }

    def updateSegment(value) {
        this.segment = value

        if(id != null && isValid()) {
            executeUpdate("UPDATE ArticleText SET Segment = ${value} WHERE Id = $id")
        } else {
            throw new IllegalStateException("Update Error: value = $value")
        }
    }



    // </editor-fold>
}
